﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
using System.Drawing;

namespace FastDeal.DoanhNghiep
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["DaDangNhap"] == null || (bool)Session["DaDangNhap"] == false)
            {
                Response.Redirect("~/Default.aspx");
            }
            if ((int)Session["LoaiThanhVien"] != 3)
            {
                Response.Redirect("~/Default.aspx");
            }

            if (!IsPostBack)
                LoadDanhSachKhuyenMai_MaDoanhNghiep();
        }

        public void LoadDanhSachKhuyenMai_MaDoanhNghiep()
        {
            KhuyenMai_BUS db = new KhuyenMai_BUS();
            int madoanhnghiep = int.Parse(Session["UserID"].ToString());
            List<KhuyenMai> list = new List<KhuyenMai>();
            list = db.LayDanhSachTheoMaDoanhNghiep_KhuyenMai(madoanhnghiep);
            GVDanhSach.DataSource = list;
            GVDanhSach.DataBind();

            //Làm màu
            int i = 0;
            foreach (GridViewRow dr in GVDanhSach.Rows)
            {
                //sửa cột ngày bắt đầu - cột 2
                DateTime dt1 = DateTime.Parse(GVDanhSach.Rows[i].Cells[2].Text);
                GVDanhSach.Rows[i].Cells[2].Text = dt1.ToShortDateString();

                //sửa cột ngày kết thúc - cột 3
                DateTime dt2 = DateTime.Parse(GVDanhSach.Rows[i].Cells[3].Text);
                GVDanhSach.Rows[i].Cells[3].Text = dt2.ToShortDateString();

                //sửa tình trạng trạng cột
                int tinhtrang = int.Parse(GVDanhSach.Rows[i].Cells[4].Text);
                if (tinhtrang == 0)
                {
                    GVDanhSach.Rows[i].Cells[4].Text = "Chưa duyệt";
                    GVDanhSach.Rows[i].Cells[0].ForeColor = Color.Red;
                    GVDanhSach.Rows[i].Cells[1].ForeColor = Color.Red;
                    GVDanhSach.Rows[i].Cells[2].ForeColor = Color.Red;
                    GVDanhSach.Rows[i].Cells[3].ForeColor = Color.Red;
                    GVDanhSach.Rows[i].Cells[4].ForeColor = Color.Red;
                    GVDanhSach.Rows[i].Cells[5].ForeColor = Color.Red;

                }
                else if (tinhtrang == 1)
                {
                    GVDanhSach.Rows[i].Cells[4].Text = "Đã duyệt";
                    GVDanhSach.Rows[i].Cells[4].ForeColor = Color.Gray;
                }
                //sửa mã doanh nghiệp thành username
                int id = int.Parse(GVDanhSach.Rows[i].Cells[5].Text);
                ThongTinTaiKhoan_BUS db2 = new ThongTinTaiKhoan_BUS();
                ThongTinTaiKhoan tt = db2.LayThongTin_ThongTinTaiKhoan(id);
                GVDanhSach.Rows[i].Cells[5].Text = tt.HoTen;

                i++;
            }
            if (Panel1.Height.Value < 1000)
            {
                Panel1.Height = 1000;
            }
        }

        protected void GVDanhSach_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVDanhSach.PageIndex = e.NewPageIndex;
            LoadDanhSachKhuyenMai_MaDoanhNghiep();
        }

        protected void GVDanhSach_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //Lấy mã để xóa
                int ma = int.Parse(GVDanhSach.Rows[e.RowIndex].Cells[0].Text);
                KhuyenMai_BUS db = new KhuyenMai_BUS();
                KhuyenMai km = new KhuyenMai();
                km.MaKhuyenMai = ma;
                db.Xoa_KhuyenMai(km);
                LoadDanhSachKhuyenMai_MaDoanhNghiep();
            }
            catch
            {

            }
        }

        protected void GVDanhSach_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}